/*
 *  Copyright 2009 mcc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package lazyjava.asset;

import org.junit.Assert;

import java.lang.reflect.Field;

import java.math.BigDecimal;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * The AssetHelper class aims to assist automatic testing. It is still under development.
 * @author mcc
 */
public class AssetHelper {
    private static int DEFAULT_SEARCH_DEPTH = 3;    // The default search depth in class, in order to avoid infinite loop

    // set of Class.class that allow Junit.assetEquals (those implemented equals function), normally they are primitive type and String
    private static Set<Class> allowedAssert = new HashSet<Class>() {
        {}
    };
    private static Set<Package> allowedAssertPackage = new HashSet<Package>() {
        {
            add(Character.class.getPackage());
            add(BigDecimal.class.getPackage());
        }
    };

    public static void assertEqualsReflection(final Object o1, final Object o2) {
        assertEqualsReflection(o1, o2, (o1 != null)
                                       ? o1.getClass().getName()
                                       : "", DEFAULT_SEARCH_DEPTH, allowedAssertPackage, allowedAssert);
    }

    public static void assertEqualsReflection(final Object o1, final Object o2, String prefix, int remainLevel,
            final Set<Package> allowedAssetPackages, final Set<Class> allowedAssetClasses) {
        System.out.println(prefix.toString());

        if (remainLevel-- < 0) {
            return;
        }

        if ((o1 == null) && (o2 == null)) {
            System.out.println("null");

            return;
        } else if ((o1 == null) && (o2 != null)) {
            throw new AssertionError(o1.getClass().getName() + " expected is null, but actual not.");
        } else if ((o2 == null) && (o1 != null)) {
            throw new AssertionError(o1.getClass().getName() + " expected is not null, but actual is.");
        } else {
            Package p1 = o1.getClass().getPackage();

            if (allowedAssetPackages.contains(p1) || allowedAssetClasses.contains(o1.getClass())) {
                Assert.assertEquals(prefix.toString(), o1, o2);
            } else {
                if (List.class.isInstance(o1)) {
                    if (((List) o1).size() != ((List) o2).size()) {
                        throw new AssertionError(prefix + " size expected is " + ((List) o1).size()
                                                 + ", but actual is " + ((List) o2).size());
                    } else {
                        Iterator i1 = ((List) o1).iterator();
                        Iterator i2 = ((List) o2).iterator();

                        for (int i = 0; i1.hasNext() && i2.hasNext(); ++i) {
                            assertEqualsReflection(i1.next(), i2.next(), prefix + "[" + i + "]", remainLevel,
                                                   allowedAssetPackages, allowedAssetClasses);
                        }
                    }
                } else if (o1 instanceof Collection) {
                    Assert.assertEquals(prefix.toString(), o1, o2);
                } else {
                    for (Field f : o1.getClass().getDeclaredFields()) {
                        if (!f.isAnnotationPresent(IgnoreAssetEquals.class)) {
                            f.setAccessible(true);

                            try {
                                assertEqualsReflection(f.get(o1), f.get(o2), prefix + "." + f.getName(), remainLevel,
                                                       allowedAssetPackages, allowedAssetClasses);
                            } catch (IllegalAccessException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }
}
